package com.jdz.gxq.info.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class HttpTookit {
	
	/**
	 * 执行一个HTTP POST请求，返回请求响应的HTML
	 * @return 返回请求响应的HTML
	 */
	public static void post(String urlStr, Map<String, String> params) throws IOException {

		URL url = new URL(urlStr);
		URLConnection connection = url.openConnection();
		connection.setDoOutput(true); 
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

		StringBuffer postData = new StringBuffer();
		if (params != null) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				postData.append(entry.getKey());
				postData.append("=");
				postData.append(entry.getValue());
				postData.append("&");
			}
		}
		postData.deleteCharAt(postData.length() -1);

		out.write(postData.toString());
		out.flush();
		out.close();

		String sCurrentLine = "";
		String result = "";

		// 得到返回值
		InputStream inputStream = connection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		while ((sCurrentLine = reader.readLine()) != null) {
			result += sCurrentLine;
		}

		System.out.println(result);
	}

	public static void main(String[] args) throws IOException {
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("method", "getEstate");

		post("http://localhost:8080/tcmsystem/mobileFacade.do", params);
	}
}
